package com.hmy.contract.service.contract.save.lease.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.hmy.ccp.starter.web.utils.UserContextUtil;
import com.hmy.contract.commons.enums.ContractActorTypeEnum;
import com.hmy.contract.manager.order.OrderFeignClientManager;
import com.hmy.contract.manager.worklfow.FlowProcessClientManager;
import com.hmy.contract.service.dto.contract.*;
import com.hmy.contract.service.dto.save.SaveLeaseContractDto;
import com.hmy.order.api.bo.request.ContractInfoUpdateApiRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author seven.qi 齐胜南
 * @version 1.0
 * @description
 * @date 2024/3/14 19:14
 */
@Slf4j
@Service
public class SaveLeaseContractBaseService {

    @Resource
    private LeaseContractFieldBuildService leaseContractFieldBuildService;

    @Resource
    private FlowProcessClientManager flowProcessClientManager;

    @Resource
    private OrderFeignClientManager orderFeignClientManager;


    protected void notifyOrder(String contractId, Long orderId) {
        ContractInfoUpdateApiRequest contractInfoUpdateApiRequest = new ContractInfoUpdateApiRequest();
        contractInfoUpdateApiRequest.setContractNo(contractId);
        contractInfoUpdateApiRequest.setOrderId(orderId);
        orderFeignClientManager.updateContractNo(contractInfoUpdateApiRequest);
    }

    protected CustomerExtensionDto convertToCustomerExtension(OrderPrimaryAndSupplyDto orderInfo) {
        return BeanUtil.copyProperties(orderInfo, CustomerExtensionDto.class);
    }

    protected String addTemplateDataDetailExtension(SaveLeaseContractDto dto) {
        List<FieldDto> fieldDtoList = leaseContractFieldBuildService.fillFieldValue(dto);
        TemplateDataDetailExtensionDto templateDataDetailExtensionDto = new TemplateDataDetailExtensionDto();

        Map<String, Object> fields = new HashMap<>();
        fieldDtoList.forEach(fieldDto -> {
            if (ObjectUtil.isNotEmpty(fieldDto.getValue())) {
                fields.put(fieldDto.getKey(), fieldDto.getValue());
            }
        });
        templateDataDetailExtensionDto.setFields(fields);
        return JSONUtil.toJsonStr(templateDataDetailExtensionDto);
    }

    protected String addLeaseContractActorDetail(SaveLeaseContractDto dto) {
        ActorDetailExtensionDto actorDetailExtensionDto = new ActorDetailExtensionDto();
        List<ContractActorDto> actorList = new ArrayList<>();
        ContractActorDto leaseActor = new ContractActorDto();
        leaseActor.setActorName(UserContextUtil.getCurrentUser().getUserName());
        leaseActor.setActorPhone(UserContextUtil.getCurrentUser().getPhone());
        leaseActor.setActorId("参与方1");
        leaseActor.setActorType(ContractActorTypeEnum.CORP.getType());

        ContractActorDto customerActor = new ContractActorDto();
        customerActor.setActorType(ContractActorTypeEnum.PERSON.getType());
        if (StrUtil.isNotEmpty(dto.getOrderInfo().getCustomerContactorIdCardNumber())) {
            customerActor.setActorIdCardNo(dto.getOrderInfo().getCustomerContactorIdCardNumber());
        }
        customerActor.setActorName(dto.getOrderInfo().getCustomerContactorName());
        customerActor.setActorPhone(dto.getOrderInfo().getCustomerContactorPhone());
        customerActor.setActorId("参与方2");

        actorList.add(leaseActor);
        actorList.add(customerActor);
        actorDetailExtensionDto.setActorDetailList(actorList);
        return JSONUtil.toJsonStr(actorDetailExtensionDto);
    }

}
